package graphics.scene;

import graphics.rendering.primitives.RenderPackage;
import graphics.rendering.GraphicsPipeline;

import java.util.ArrayList;
import java.util.List;

public class SceneNode {
	
	private SceneNode parent;
	
	private List<SceneNode> children;
	
	private boolean visible;
	
	private RenderPackage renderPackage;
	
	/**
	 * Orthogonal position matrix.
	 */
	private float[] transform;
	
	public SceneNode(SceneNode parent) {
		this.parent = parent;
		transform = new float[16];
	}
	
	public SceneNode createChild() {
		if(children == null) {
			children = new ArrayList<SceneNode>();
		}
		
		SceneNode child = new SceneNode(this);
		children.add(child);
		return child;
	}
	
	public List<SceneNode> getChildren() {
		if(children == null) {
			children = new ArrayList<SceneNode>();
		}
		
		return children;
	}
	
	public SceneNode getParent() {
		return parent;
	}
	
	public void setContent(RenderPackage renderPackage) {
		this.renderPackage = renderPackage;
	}
	
	public boolean isVisible() {
		return visible;
	}
	
	public void setVisible(boolean visible) {
		this.visible = visible;
	}
	
	public float[] getMatrix() {
		return transform;
	}
	
	public void render(GraphicsPipeline graphics) {
		// Render self
		if(visible && renderPackage != null) {
			graphics.submitPrimitive(renderPackage);
		}
		
		// Recursively render children
		if(children != null) {
			for(int i = 0; i < children.size(); i++) {
				children.get(i).render(graphics);
			}
		}
	}
}
